package org.jiamandan.com.strategy.listener;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.strategy.IDXContextListener;
import org.jiamandan.com.utils.DXObjectUtils;
import org.springframework.stereotype.Service;


@Service(value = DXConstant.Service.CMD_AC0)
@AllArgsConstructor
@Slf4j
/**
 * 订单停止后查询状态
 */
public class DXAC0QueryChargePileStatusService implements IDXContextListener {

    @Override
    public void doExecute(DXBasicDto dto, Channel ctx) throws Exception {
        log.info("接收嘉满电-DX获取AC0设备信息指令(CMD=AC0)【"+dto.getDeviceNo()+"】报文:" + JSON.toJSONString(dto));
        String payload = dto.getData();
        String chargeStatus = payload.substring(0, 2);
        String calculateModel = payload.substring(2, 4);
        String curElectric = payload.substring(4, 8);
        String sumChargeTime = payload.substring(8, 12);
        String totalElectric = payload.substring(12, 16);
        String stopReason = DXObjectUtils.getStopReason(payload.substring(12, 16));
        if(stopReason.equals("00")){
            return;
        }
        //01:达到预设时间，02：充电电量到，03：充满，04：充满后继续充电，05：充满后停止充电，06：充满后继续充电，07：充满后停止充电，08：充满后继续充电，09：
        log.info("DX获取AC0设备信息指令，充电状态={},充电模式={},当前的电流={},已用时间={},已用电量={},停止充电的原因={}",
                chargeStatus,calculateModel,curElectric,sumChargeTime,totalElectric,stopReason);
        /*if(false){
            QueryWrapper<OrderCharge> query = Wrappers.<OrderCharge>query();
            query.lambda().eq(OrderCharge::getChargePileNo, dto.getDeviceNo());
            query.lambda().eq(OrderCharge::getChargeGunNo, gunNo);
            query.lambda().eq(OrderCharge::getOrderStatus, BizConstant.OrderStatus.CHARGING);
            query.lambda().eq(OrderCharge::getStatus, BizConstant.Status.ACTIVE);
            query.lambda().eq(OrderCharge::getIsDeleted, 0);
            query.lambda().orderByDesc(OrderCharge::getUpdateTime);
            List<OrderCharge> orderCharges = iOrderChargeService.list(query);
            if (orderCharges != null && orderCharges.size() == 1) {
                OrderCharge orderCharge = orderCharges.get(0);
                Integer orderStatus = orderCharge.getChargeDuration()*60;
                iOrderChargeService.stopDxCharge(orderCharges.get(0),"07",sumChargeTime,orderCharge.getTotalElectric());
            }
        }*/
    }
}
